using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System.Collections;
using Miao;

namespace Miao.UI
{
    [SourcesPath("Assets/Plugins/miao-framework/UIPrefab/LoadingWait.prefab")]
    public class LoadingWait : UISceneLoadingPanel
    {
        public Image Gear;
        public Image Gear2;
        public TextMeshProUGUI StatusTest;
        public Image ImageProgressBar;
        public Slider ProgressBar;
        //public RunningCoroutine runningCoroutine;

        protected void Start()
        {
            //runningCoroutine = CoroutineStarter.Start(TextStart());
        }
        protected override void LoadingUpdate()
        {
            Gear.transform.Rotate(Vector3.forward * Time.deltaTime * 100);
            Gear2.transform.Rotate(-Vector3.forward * Time.deltaTime * 100);
            ImageProgressBar.fillAmount = sceneAsync.progress;
            ProgressBar.value = sceneAsync.progress;
        }
        private Tweener handle;
        protected void OnDestroy()
        {
            handle.Kill();
        }

        public override IMotion LoadStart()
        {
            return new FadeUIMotion();
        }

        public override IMotion LoadComplete()
        {
            return new FadeUIMotion();
        }
        protected override void LoadCompleted(AsyncOperation obj)
        {
            base.LoadCompleted(obj);
            TextShowComplete();
        }
        //private WaitForSeconds waitForSeconds = new WaitForSeconds(1);
        protected IEnumerator TextStart()
        {
            int i = 0;
            StringBuilder stringBuilder = new StringBuilder(20);
            while (!sceneAsync.isDone)
            {
                i++;
                yield return new WaitForSeconds(1);
                stringBuilder.Clear();
                for (int v = i % 4; v > 0; i--)
                {
                    stringBuilder.Append(".");
                }
                StatusTest.text = "载入中" + stringBuilder;
            }
        }


        protected void TextShowComplete()
        {
            //runningCoroutine.Stop();
            StatusTest.text = "载入完成，任意键继续";
            handle = StatusTest.DOFade(1, 1);
        }

    }
}